package com.openbravo.data.loader;

import com.openbravo.basic.BasicException;

import java.util.ArrayList;
import java.util.Date;

public class NormalBuilder
  implements ISQLBuilderStatic
{
  private String m_sSentence;

  public NormalBuilder(String paramString)
  {
    this.m_sSentence = paramString;
  }

  public String getSQL(SerializerWrite paramSerializerWrite, Object paramObject)
    throws BasicException
  {
	  //ati 
	  if(paramObject==null)
	  {
		   //NormalParameter localNormalParameter = new NormalParameter(this.m_sSentence);
		   return m_sSentence;
	  }
		 
    NormalParameter localNormalParameter = new NormalParameter(this.m_sSentence);
    if (paramSerializerWrite != null)
    	//ati pb12
    	if(!this.m_sSentence.startsWith("SELECT "))
    		paramSerializerWrite.writeValues(localNormalParameter, paramObject);
    
    
   //ati  
    if(this.m_sSentence.startsWith("SELECT ")&& this.m_sSentence.contains("?"))
    	this.m_sSentence=this.m_sSentence.replace("?", "'"+paramObject.toString()+"'");
    if(this.m_sSentence.startsWith("DELETE ")&& this.m_sSentence.contains("?"))
    	this.m_sSentence=this.m_sSentence.replace("?", "'"+paramObject.toString()+"'");
    System.out.println("---sql"+this.m_sSentence);
    String sentence_sql = localNormalParameter.getSentence();
    if(sentence_sql.trim().equals(""))
    	sentence_sql=this.m_sSentence;
    
    if(sentence_sql.contains("?") )
    	sentence_sql= this.m_sSentence;
	return sentence_sql;
  }

  private static class NormalParameter
    implements DataWrite
  {
    private String m_sSentence;
    private ArrayList m_aParams;

    public NormalParameter(String paramString)
    {
      this.m_sSentence = paramString;
      this.m_aParams = new ArrayList();
    }

    public void setDouble(int paramInt, Double paramDouble)
      throws BasicException
    {
      ensurePlace(paramInt - 1);
      this.m_aParams.set(paramInt - 1, DataWriteUtils.getSQLValue(paramDouble));
    }

    public void setBoolean(int paramInt, Boolean paramBoolean)
      throws BasicException
    {
      ensurePlace(paramInt - 1);
      this.m_aParams.set(paramInt - 1, DataWriteUtils.getSQLValue(paramBoolean));
    }

    public void setInt(int paramInt, Integer paramInteger)
      throws BasicException
    {
      ensurePlace(paramInt - 1);
      this.m_aParams.set(paramInt - 1, DataWriteUtils.getSQLValue(paramInteger));
    }

    public void setString(int paramInt, String paramString)
      throws BasicException
    {
      ensurePlace(paramInt - 1);
      this.m_aParams.set(paramInt - 1, DataWriteUtils.getSQLValue(paramString));
    }

    public void setTimestamp(int paramInt, Date paramDate)
      throws BasicException
    {
      ensurePlace(paramInt - 1);
      this.m_aParams.set(paramInt - 1, DataWriteUtils.getSQLValue(paramDate));
    }

    public void setBytes(int paramInt, byte[] paramArrayOfByte)
      throws BasicException
    {
      throw new BasicException(LocalRes.getIntString("exception.noparamtype"));
    }

    public void setObject(int paramInt, Object paramObject)
      throws BasicException
    {
      ensurePlace(paramInt - 1);
      this.m_aParams.set(paramInt - 1, DataWriteUtils.getSQLValue(paramObject));
    }

    private void ensurePlace(int paramInt)
    {
      this.m_aParams.ensureCapacity(paramInt);
      while (paramInt >= this.m_aParams.size())
        this.m_aParams.add(null);
    }

    public String getSentence()
    {
      StringBuffer localStringBuffer = new StringBuffer();
      int i = 0;
      int j;
      for (int k = 0; (j = this.m_sSentence.indexOf('?', k)) > 0; k = j + 1)
      {
        localStringBuffer.append(this.m_sSentence.substring(k, j));
        if ((i < this.m_aParams.size()) && (this.m_aParams.get(i) != null))
          localStringBuffer.append(this.m_aParams.get(i));
        else
          localStringBuffer.append(DataWriteUtils.getSQLValue((Object)null));
        i++;
        localStringBuffer.append(this.m_sSentence.substring(k));
      }
 //     localStringBuffer.append(this.m_sSentence.substring(k));
      return localStringBuffer.toString();
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.data.loader.NormalBuilder
 * JD-Core Version:    0.6.0
 */